package com.loan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loan.entity.Apply;
import com.loan.entity.Product;
import com.loan.exeception.AppException;
import com.loan.exeception.AppExceptionCodeMsg;
import com.loan.mapper.ApplyMapper;
import com.loan.mapper.ProductMapper;
import com.loan.service.ApplyService;
import com.loan.service.SendSmsService;
import com.loan.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* @author z
* @description 针对表【apply】的数据库操作Service实现
* @createDate 2023-04-21 14:51:22
*/

@Slf4j
@Service
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements ApplyService {
    @Resource
    private ApplyMapper applyMapper;
    @Resource
    private ProductMapper productMapper;

    @Resource
    private SendSmsService sendSmsService;



    @Override
    public ResponseResult<Object> addProAndApply(Apply apply) {
        //补充proApplyVO里的字段
        String pid = apply.getPid();
        if (pid == null ) {
            throw new AppException(AppExceptionCodeMsg.PRODUCT_NOT_FOUND);
        }
        //判断是否输入手机号
        if (apply.getPhone()==null|| "".equals(apply.getPhone())){
            throw new AppException(AppExceptionCodeMsg.PHONE_NUMBER_IS_NULL);
        }else {
            //验证码比对
            boolean b = sendSmsService.checkSms(apply.getPhone(), apply.getCode());
            if (!b){
                throw new AppException(AppExceptionCodeMsg.CODE_NUMBER_IS_ERROR);
            }else {
                Product product = productMapper.selectById(pid);
                //产品名称
                apply.setPname(product.getPname());
                //申请贷款的金额
                BigDecimal amount = apply.getAmount();
                //产品的最小贷款金额
                BigDecimal min = product.getMin();
                //产品的最大贷款金额
                BigDecimal max = product.getMax();
                //判断申请贷款金额是否合法---amount<max&&amount>min
                if (amount.compareTo(max) <= 0 && amount.compareTo(min) >= 0){
                    applyMapper.insert(apply);
                }else {
                    throw new AppException(AppExceptionCodeMsg.APPLY_LOAN_ILLEGAL);
                }
                return ResponseResult.success("申请贷款成功");
            }
        }
    }

    /**
     * 贷款信息轮播
     * @return
     */
    @Override
    public List showProApply() {
        //查询数据库所有数据
        LambdaQueryWrapper<Apply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Apply::getUpdateTime);
        queryWrapper.last("limit 10");
        List<Apply> applies = applyMapper.selectList(queryWrapper);
        //新建一个list
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        //遍历查询出的apply表中的数据
        for (Apply apply : applies) {
            HashMap<String, Object> map = new HashMap<>();
            String phone = apply.getPhone();
            StringBuilder maskedPhone = new StringBuilder();
            char[] phoneChars = phone.toCharArray();
            for (int i = 0; i < phoneChars.length; i++) {
                if (i >= 3 && i <= 6) {
                    maskedPhone.append('*');
                } else {
                    maskedPhone.append(phoneChars[i]);
                }
            }
            map.put("tel", maskedPhone.toString());
            map.put("name",apply.getName().substring(0,1));
            map.put("pro",apply.getPname());
            map.put("money",apply.getAmount());
            list.add(map);
        }
        return list;
    }

    @Override
    public void test() {
        String phone="15683006805";
        String code="21313";
        /*
        boolean sendSmsResult = aliYunSmsService.sendSms(phone,code, SmsConstant.SIGN_NAME, SmsConstant.COMMON_TEMPLATE_CODE);
        if (!sendSmsResult){
            throw new SmsException("send sms failed");
        }*/
    }


}




